package com.senter.rsatest;

import java.util.Map;

import security.AESUtils;
import security.RSAUtils;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	byte[] publicKey;
	byte[] privateKey;

	public void onClick(View v) {
		new Thread() {

			@Override
			public void run() {
				doTask();
				super.run();
			}

		}.start();

	}

	private void doTask() {

		// 客户端产生一对密钥
		try {
			Map<String, Object> keyMap = RSAUtils.genKeyPair();
			publicKey = RSAUtils.getPublicKey(keyMap);
			privateKey = RSAUtils.getPrivateKey(keyMap);
			Log.i("test", "公钥-->" + new String(publicKey));
			Log.i("test", "私钥-->" + new String(privateKey));
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

		// 发送公钥给服务器
		// 服务器通过客户端公钥加密生成密文
		String seed = "Is's a seed";// 32位随机数
		byte[] seedServer = seed.getBytes();
		byte[] AESKeyServer;
		byte[] encodedData;
		try {
			AESKeyServer = AESUtils.getRawKey(seedServer);// 产生AES密钥以便之后通信
			encodedData = RSAUtils.encryptByPublicKey(seedServer, publicKey);// 通过客户端公钥加密
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

		// 客户端收到密文用自己的私钥解密
		byte[] seedClient;
		byte[] AESKeyClient;
		try {
			seedClient = RSAUtils.decryptByPrivateKey(encodedData, privateKey);
			AESKeyClient = AESUtils.getRawKey(seedClient);// 产生AES密钥以便之后通信
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

		// 通过AES密钥与服务器通信
		System.err.println("AES解密");
		String source = "这是一行没有任何意义的文字，你看完了等于没看，不是吗？";
		System.out.println("原文字：\r\n" + source);
		byte[] secretStrs;
		try {
			secretStrs = AESUtils.encrypt(AESKeyClient, source);
			System.out.println("加密后：\r\n" + new String(secretStrs));

		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

		// 服务器收到密文解密
		byte[] target;
		try {
			target = AESUtils.decrypt(AESKeyServer, secretStrs);
			System.out.println("解密后: \r\n" + new String(target));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void testRSA() throws Exception {
		System.err.println("公钥加密——私钥解密");
		String source = "这是一行没有任何意义的文字，你看完了等于没看，不是吗？";
		System.out.println("\r加密前文字：\r\n" + source);
		byte[] data = source.getBytes();
		byte[] encodedData = RSAUtils.encryptByPublicKey(data, publicKey);
		System.out.println("加密后文字：\r\n" + new String(encodedData));
		byte[] decodedData = RSAUtils.decryptByPrivateKey(encodedData, privateKey);
		String target = new String(decodedData);
		System.out.println("解密后文字: \r\n" + target);
	}

	private void testRSASign() throws Exception {
		System.err.println("私钥加密——公钥解密");
		String source = "这是一行测试RSA数字签名的无意义文字";
		System.out.println("原文字：\r\n" + source);
		byte[] data = source.getBytes();
		byte[] encodedData = RSAUtils.encryptByPrivateKey(data, privateKey);
		System.out.println("加密后：\r\n" + new String(encodedData));
		byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey);
		String target = new String(decodedData);
		System.out.println("解密后: \r\n" + target);
		System.err.println("私钥签名——公钥验证签名");
		String sign = RSAUtils.sign(encodedData, privateKey);
		System.err.println("签名:\r" + sign);
		boolean status = RSAUtils.verify(encodedData, publicKey, sign);
		System.err.println("验证结果:\r" + status);
	}
}
